package com.liuyang.homework.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.liuyang.homework.Activity.NewsDetailActivity;
import com.liuyang.homework.Bean.News;
import com.liuyang.homework.R;

import java.util.List;

/**
 * Created by liuyang on 2017/1/14.
 */

public class NewsAdapter extends RecyclerView.Adapter<NewsHolder> {

    List<News> newsList;
    Context context;

    public NewsAdapter(List<News> newsList,Context context){
        this.newsList = newsList;
        this.context = context;
    }

    @Override
    public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);

        return new NewsHolder(view);
    }

    @Override
    public void onBindViewHolder(NewsHolder holder, final int position) {
        holder.tvTitle.setText(newsList.get(position).getTitle());
        holder.tvFrom.setText(newsList.get(position).getSource());
        holder.tvTime.setText(newsList.get(position).getCreatedAt());
        Glide.with(context).load(newsList.get(position).getImage()).into(holder.ivNewImage);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("news",newsList.get(position));
                intent.putExtra("news",bundle);
                context.startActivity(intent);
            }
        });
        holder.btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               // NewsLab.get(context).addNews(newsList.get(position));
            }
        });

        holder.btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                NewsLab.get(context).deleteNews(newsList.get(position));
//                newsList.remove(position);
//                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }
}


class NewsHolder extends RecyclerView.ViewHolder{

    ImageView ivNewImage;
    TextView tvTitle,tvFrom,tvTime;
    View btDelete,btSave;

    public NewsHolder(View itemView) {
        super(itemView);
        ivNewImage = (ImageView) itemView.findViewById(R.id.iv_news_image);
        tvFrom= (TextView) itemView.findViewById(R.id.tv_news_from);
        tvTime = (TextView) itemView.findViewById(R.id.tv_news_time);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_news_title);
        btDelete=itemView.findViewById(R.id.bt_delete);
        btSave=itemView.findViewById(R.id.bt_save);
    }
}
